import commonData from '@/hooks/commonData'
import { lxlxlOptions, lxsyzlOptions } from '@/components/common'
import { deleteDetailApi } from '@/api/contract'
import AOS from 'aos'

export default function cardBoxData(props, handleSuccess) {
    const { setContractData } = commonData()

    const isTableShow = ref(false)
    const addContractRef = ref()

    function handleChangeCollapse() {
        isTableShow.value = !isTableShow.value
        setTimeout(() => {
            AOS.refresh()
        }, 300)
    }

    function handleAddContract(row, type) {
        const typeMapping = {
            '0': '常规事项',
            '1': '新增',
            '2': row.lxStatus !== 9 ? '编辑' : '补充',
            '3': '其他',
            '4': '销项',
            '5': '查看'
        }

        const lxsyzlType = row.lxsyzl >= 0 ? String(row.lxsyzl) : ''

        const data = { ...row, lxsyzl: lxsyzlType }
        setContractData({
            lxlx: String(props.refIndex),
            lxlxStr: lxlxlOptions[props.refIndex],
            lxsyzl: type == 5 ? String(row.lxsyzl) : type,
            lxsyzlStr: typeMapping[type] || ''
        })
        addContractRef.value.show(data)
    }

    function handleDelete(row, uuid) {
        ElMessageBox.confirm(`是否删除合同 [ ${row.lxmc} ] ?`, '警告', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        }).then(function() {
            deleteDetailApi({
                uuid,
                'lxUuid': row.lxUuid
            }).then(data => {
                if (data.code == 0) {
                    ElMessage.success('合同删除成功')
                    handleSuccess()
                }
            })
        })
    }

    return {
        addContractRef,
        handleChangeCollapse, isTableShow,
        handleAddContract,
        handleDelete
    }
}
